package com.ey12.db.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmMoneyRecord;

/**
 * Data access object (DAO) for domain model class GmMoneyRecord.
 * 
 * @see com.ey12.db.domain.GmMoneyRecord
 * @author MyEclipse Persistence Tools
 */

public class GmMoneyRecordDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmMoneyRecordDAO.class);
	// property constants
	public static final String GET_MONEY = "getMoney";
	public static final String CREATE_DATE = "createDate";
	public static final String STATE = "state";
	public static final String GET_TYPE = "getType";

	protected void initDao() {
		// do nothing
	}
	
//	
//	public List getNewValueOrder(String username){
//	try {
//		String queryString = "from GmMoneyRecord gm where gm.gmUser.userName='"+username+"' and gm.state=1 and gm.getType=1";
//		return getHibernateTemplate().find(queryString);
//	} catch (RuntimeException re) {
//		log.error("find all failed", re);
//		throw re;
//	}
//}

	
	public List findFundDetial(GmMoneyRecord  gm){
		try {
			String queryString = "from GmMoneyRecord gm where gm.gmUser.userName='"+gm.getGmUser().getUserName()+"'";
			if(gm.getState()!=null&&gm.getState()!=-1){
				queryString+=" and gm.state='"+gm.getState()+"'";				
			}
			if(gm.getGetType()!=null&&gm.getGetType()!=-1){
				queryString+=" and gm.getType='"+gm.getGetType()+"'";
			}
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public void saveOrUpdate(GmMoneyRecord transientInstance) {
		log.debug("saving GmMoneyRecord instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GmMoneyRecord persistentInstance) {
		log.debug("deleting GmMoneyRecord instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GmMoneyRecord findById(java.lang.Integer id) {
		log.debug("getting GmMoneyRecord instance with id: " + id);
		try {
			GmMoneyRecord instance = (GmMoneyRecord) getHibernateTemplate()
					.get("com.ey12.db.domain.GmMoneyRecord", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmMoneyRecord instance) {
		log.debug("finding GmMoneyRecord instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmMoneyRecord instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from GmMoneyRecord as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByGetMoney(Object getMoney) {
		return findByProperty(GET_MONEY, getMoney);
	}

	public List findByCreateDate(Object createDate) {
		return findByProperty(CREATE_DATE, createDate);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByGetType(Object getType) {
		return findByProperty(GET_TYPE, getType);
	}

	public List findAll() {
		log.debug("finding all GmMoneyRecord instances");
		try {
			String queryString = "from GmMoneyRecord";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findCashDraw() {
		log.debug("finding all GmMoneyRecord instances");
		try {
			String queryString = "from GmMoneyRecord g where g.getType=2 and g.state=1";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	
	public List findTheCashDraw(String qq,String username){
		log.debug("finding all GmMoneyRecord instances");
		try {
			String queryString = "from GmMoneyRecord g where g.getType=2 and g.state=1";
			if(qq!=null&qq!=""){
				queryString+=" and g.gmUser.qqNum='"+qq+"'";
			}
			if(username!=null&&username!=""){
				queryString+=" and g.gmUser.userName='"+username+"'";
			}
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findChargeValue() {
		log.debug("finding all GmMoneyRecord instances");
		try {
			String queryString = "from GmMoneyRecord g where g.getType=1 and g.state=1";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findTheChargeValue(String qq,String username){
		log.debug("finding all GmMoneyRecord instances");
		try {
			String queryString = "from GmMoneyRecord g where g.getType=1 and g.state=1";
			if(qq!=null&qq!=""){
				queryString+=" and g.gmUser.qqNum='"+qq+"'";
			}
			if(username!=null&&username!=""){
				queryString+=" and g.gmUser.userName='"+username+"'";
			}
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public GmMoneyRecord merge(GmMoneyRecord detachedInstance) {
		log.debug("merging GmMoneyRecord instance");
		try {
			GmMoneyRecord result = (GmMoneyRecord) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GmMoneyRecord instance) {
		log.debug("attaching dirty GmMoneyRecord instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmMoneyRecord instance) {
		log.debug("attaching clean GmMoneyRecord instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmMoneyRecordDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (GmMoneyRecordDAO) ctx.getBean("GmMoneyRecordDAO");
	}
}